package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import edu.sctu.yueding.activitys.ActivityInformationActivity;
import edu.sctu.yueding.bean.HomeHeadBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.SharePreference;

/**
 * Created by zhengsenwen on 2016/8/2.
 */
public class ViewPagerAdapter extends PagerAdapter {

    private List<View> viewList;
    private Activity activity;
    private List<HomeHeadBean> list;

    public ViewPagerAdapter(List<View> viewList,Activity activity,List<HomeHeadBean> list) {
        this.viewList = viewList;
        this.activity = activity;
        this.list = list;
    }


    @Override
    public int getCount() {
        if (viewList != null && viewList.size() > 0) {
            return viewList.size();
        } else {
            return 0;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == (View) o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        object = null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

                viewList.get(position).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("matchContent", list.get(position).getContent());
                SharePreference.getInstance(activity).setCache(CacheConfig.CACHE_MATCH_ID,list.get(position).getId()+"");

                if (list.get(position).getPrice() == 0.0f)
                    SharePreference.getInstance(activity).setCache(CacheConfig.IS_PRICE, true);
                else SharePreference.getInstance(activity).setCache(CacheConfig.IS_PRICE, false);
                JumpUtils.jumpActivity(activity, ActivityInformationActivity.class,bundle);
            }
        });
        container.addView(viewList.get(position));
        return viewList.get(position);
    }
}
